from django.db import models
from datetime import datetime
from goods.models import Goods
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.


class ShoppingCart(models.Model):
    """
    购物车
    """
    user = models.ForeignKey(User, db_constraint=False, help_text='用户')
    goods = models.ForeignKey(Goods, db_constraint=False, help_text='商品')
    nums = models.IntegerField(default=0, help_text='购买数量')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = '购物车'
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s(%d)".format(self.goods.name, self.nums)


class OrderInfo(models.Model):
    """
    订单
    """
    OrderStatus = (
        ("WAIT_BUYER_PAY", "交易创建，等待买家付款"),
        ("TRADE_CLOSED", "未付款交易超时关闭"),
        ("TRADE_SUCCESS", "交易支付成功"),
        ("TRADE_FINISHED", "交易结束")
    )
    user = models.ForeignKey(User, db_constraint=False, help_text='用户')
    order_sn = models.CharField(unique=True, max_length=30, null=True, blank=True, help_text='订单号')
    trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, help_text='交易号')
    pay_status = models.CharField(choices=OrderStatus, default="WAIT_BUYER_PAY", max_length=30, help_text='支付状态')
    post_script = models.CharField(null=True, blank=True, max_length=200, help_text='订单留言')
    order_mount = models.FloatField(default=0.1, help_text='订单金额')
    pay_time = models.DateTimeField(null=True, blank=True, help_text='支付时间')

    # 用户信息
    address = models.CharField(max_length=100, null=True, blank=True, help_text='收货地址')
    signer_name = models.CharField(max_length=20, null=True, blank=True, help_text='收货人')
    signer_mobile = models.CharField(max_length=11, null=True, blank=True, help_text='联系电话')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "订单"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.order_sn


class OrderGoods(models.Model):
    """
    订单商品详情
    """
    order = models.ForeignKey(OrderInfo, db_constraint=False, help_text='订单信息', related_name='goods')
    goods = models.ForeignKey(Goods, db_constraint=False, help_text='商品')
    goods_num = models.IntegerField(default=1, help_text='购买数量')
    add_time = models.DateTimeField(default=datetime.now, help_text='添加时间')

    class Meta:
        verbose_name = "订单商品详情"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.order.order_sn


